<?php

namespace app\admin\controller\financing;

use app\common\controller\Backend;
use app\common\library\Auth;
use think\Db;
use think\Env;

/**
 * 周边产品收费记录-商品
 *
 * @icon fa fa-user
 */
class Products extends Backend
{


    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;
    protected $models = null;
    protected $categorylist = [];
    private $redis;

    public function _initialize()
    {
        parent::_initialize();
        //$this->model = new \app\admin\model\Registration;
        $this->model = new \app\admin\model\Orders;
        $this->redis = redis(Env::get('redis.host'), Env::get('redis.port'), Env::get('redis.pwd'), Env::get('redis.db'));
    }

    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                //->with(["products"=>['id','name','type']])
                ->with([
                    'products' => function ($query) {
                        $query->withField('id,name,type');
                    }
                ])
                ->where($where)
                ->where('status','in',[1,3])
                ->where('orders.type',1)
                ->order('id', 'desc')
                ->paginate($limit);

            if (!empty($list->items())){
                foreach ($list->items() as $k=>$v){
                    if ($v->is_send == 1){
                        $list->items()[$k]['is_send'] = '需要邮寄';
                    }elseif ($v->is_send == 2){
                        $list->items()[$k]['is_send'] = '不需要邮寄';
                    }else{
                        $list->items()[$k]['is_send'] = '';
                    }
                    $list->items()[$k]['money'] = $v->money/100;
                    //$list->items()[$k]['msg'] = $v->contacts.';'.$v->phone.';'.$v->site;


                }
            }


            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 发货
     * @param $ids
     * @return string
     * @throws \think\Exception
     *
     */
    public function edits($ids){
        $this->view->assign("row", $ids);
        return $this->view->fetch();
    }



    /**
     * 发货操作
     *
     */
    public function editss(){
        $arr = request()->param();

        if (empty($arr['expressage'])){
            $this->error('快递公司不能为空');
        }

        if (empty($arr['number'])){
            $this->error('快递单号不能为空');
        }

        $data = Db::table('fa_orders')
            ->where('id',$arr['id'])
            ->find();

        if (empty($data)){
            $this->error('订单数据为空');
        }


        $update = Db::table('fa_orders')
            ->where('id',$arr['id'])
            ->update([
                'expressage' => $arr['expressage'],
                'number' => $arr['number'],
                'status' => 3,
                'update_time' => time(),
            ]);


        if ($update !== false){
            $this->success('成功');
        }else{
            $this->error('失败');
        }


    }







}
